package org.hljdrl.daf.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.hljdrl.daf.test.ModuleTest;
import org.pojoxml.core.PojoXml;
import org.pojoxml.core.PojoXmlFactory;

import android.content.Context;

public class JavaToXmlTest extends ModuleTest {
	private File xmlFile = null;

	public JavaToXmlTest(Context ctx) {
		super(ctx);
		setName("JavaToXmlTest");
		if (getContext() != null) {
			File outFile = new File(getContext().getCacheDir(),
					"JavaToXml");
			xmlFile = outFile;
			try {
				xmlFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public String runEncode() {

		PojoXml pojoxml0 = PojoXmlFactory.createPojoXml();
		Employee employee0 = new Employee();
		Address address0 = new Address();
		employee0.setName("Mike");
		employee0.setAge(45);
		employee0.setSalary(20000.45);
		address0.setAddress1("Sagar");
		address0.setAddress2("Nerul");
		address0.setCity("Mumbai");
		address0.setZip(002345);
		employee0.setAddress(address0);
		String xml = pojoxml0.getXml(employee0);
		System.out.println(xml);
		if (xmlFile != null) {
			try{
			xmlFile.createNewFile();
			FileOutputStream out = new FileOutputStream(xmlFile);
//			pojoxml0.saveXml(employee0, xmlFile.toString());
			out.write(xml.getBytes());
			out.flush();
			out.close();
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return xml;
	}

	@Override
	public String runDecode() {
		StringBuffer mSm = new StringBuffer();
		PojoXml pojoXml = PojoXmlFactory.createPojoXml();
		//
		mSm.append('\n');
		pojoXml.addClassAlias(Employee.class, "EmployeeInfo");
		Employee employee = null;
		try{
		if (xmlFile != null) {
			employee = (Employee) pojoXml.getPojoFromFile(
					 xmlFile.toString(), Employee.class);
		}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		mSm.append(employee);
		//---
		return mSm.toString();
	}

}
